package com.useful.web.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.useful.core.jackson.FieldSerializerModule;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author : wenchao.long
 * @date : Created in 2021/9/19 20:42 周日
 * @description :
 */
@Configuration
@RequiredArgsConstructor
@ConditionalOnClass({ObjectMapper.class})
@AutoConfigureBefore(ErrorMvcAutoConfiguration.class)
public class WebMvcConfig implements WebMvcConfigurer, InitializingBean {

    private final ObjectMapper obj;

    private final ServerProperties serverProperties;

    @Override
    public void afterPropertiesSet() {
        if (obj != null) {
            obj.registerModule(new FieldSerializerModule());
        }
    }

    /**
     * 开启404 自定义返回格式
     */
    @Bean
    public BasicErrorController errorController() {
        return new CustomerErrorController(serverProperties);
    }
}
